package com.finupgroup.postloan.generator.config;

import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.finupgroup.postloan.generator.PropUtils;
import com.finupgroup.postloan.generator.config.rules.NamingStrategy;

/**
 * 策略配置项
 *
 * @author YueQingwei
 * @since 2018-04-11
 */
public class StrategyConfig {

    /**
     * 是否大写命名
     */
    private boolean isCapitalMode = true;

    /**
     * 是否跳过视图
     */
    private boolean skipView = false;

    /**
     * 数据库表映射到实体的命名策略
     */
    private NamingStrategy naming = NamingStrategy.underline_to_camel;
    /**
     * 数据库表字段映射到实体的命名策略<br/>
     * 未指定按照 naming 执行
     */
    private NamingStrategy columnNaming = null;

    /**
     * 表前缀
     */
    private String[] tablePrefix;

    /**
     * 字段前缀
     */
    private String[] fieldPrefix;

    /**
     * 自定义基础的Entity类，公共字段
     */
    private String[] superEntityColumns = ConstVal.SUPER_ENTITY_COLUMNS;

    /**
     * 需要包含的表名（与exclude二选一配置）
     */
    private String[] include = null;

    /**
     * 需要排除的表名
     */
    private String[] exclude = null;
    /**
     * 【实体】是否生成字段常量（默认 true）<br>
     * -----------------------------------<br>
     * public static final String ID = "test_id";
     */
    private boolean entityColumnConstant = true;

    /**
     * 【实体】是否为lombok模型（默认 false）<br>
     * <a href="https://projectlombok.org/">document</a>
     */
    private boolean entityLombokModel = false;

    /**
     * Boolean类型字段是否移除is前缀（默认 false）<br>
     * 比如 : 数据库字段名称 : 'is_xxx',类型为 : tinyint. 在映射实体的时候则会去掉is,在实体类中映射最终结果为 xxx
     */
    private boolean entityBooleanColumnRemoveIsPrefix = false;

    /**
     * 乐观锁属性名称
     */
    private String versionFieldName;

    /**
     * 逻辑删除属性名称
     */
    private String logicDeleteFieldName;

    public StrategyConfig() {
        this.tablePrefix = new String[]{PropUtils.get("table.prefix")};
        this.include = PropUtils.get("table.include").split(",");
        this.entityLombokModel = PropUtils.getBoolean("lombok");
        this.entityColumnConstant = PropUtils.getBoolean("entity.column_constant");
    }

    /**
     * <p>
     * 大写命名、字段符合大写字母数字下划线命名
     * </p>
     *
     * @param word 待判断字符串
     * @return
     */
    public boolean isCapitalModeNaming(String word) {
        return isCapitalMode && StringUtils.isCapitalMode(word);
    }

    /**
     * <p>
     * 表名称包含指定前缀
     * </p>
     *
     * @param tableName 表名称
     * @return
     */
    public boolean containsTablePrefix(String tableName) {
        if (null != tableName) {
            String[] tps = getTablePrefix();
            if (null != tps) {
                for (String tp : tps) {
                    if (tableName.contains(tp)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public boolean isCapitalMode() {
        return isCapitalMode;
    }

    public boolean isSkipView() {
        return skipView;
    }

    public NamingStrategy getNaming() {
        return naming;
    }

    public NamingStrategy getColumnNaming() {
        if (null == columnNaming) {
            // 未指定以 naming 策略为准
            return naming;
        }
        return columnNaming;
    }

    public String[] getTablePrefix() {
        return tablePrefix;
    }

    public boolean includeSuperEntityColumns(String fieldName) {
        if (null != superEntityColumns) {
            for (String column : superEntityColumns) {
                if (column.equals(fieldName)) {
                    return true;
                }
            }
        }
        return false;
    }

    public String[] getInclude() {
        return include;
    }

    public String[] getExclude() {
        return exclude;
    }

    public boolean isEntityColumnConstant() {
        return entityColumnConstant;
    }

    public boolean isEntityLombokModel() {
        return entityLombokModel;
    }

    public boolean isEntityBooleanColumnRemoveIsPrefix() {
        return entityBooleanColumnRemoveIsPrefix;
    }

    public String getLogicDeleteFieldName() {
        return logicDeleteFieldName;
    }

    public String getVersionFieldName() {
        return versionFieldName;
    }

    public String[] getFieldPrefix() {
        return fieldPrefix;
    }
}
